/********************************************************************************
Title: Data Preparation for Rwanda/Ghana/Uganda Sample
		Define time points
		Define variables to use in each index
		Define survey round to use as reference round
********************************************************************************/
macro drop vars

loc ref round == 1
*----------------------------------

use  "${raw}/rwanda/rwanda_desc.dta", clear

sort pid date
collapse (firstnm) age ses female, by(pid)
tempfile desc
save `desc'

use  "${raw}/rwanda/rwanda.dta", clear


keep if !mi(pid)
keep if !mi(round)
keep if !mi(date)

merge m:1 pid using `desc', nogen keep(1 3)

foreach var of varlist female ses {
	gen mi_`var' = mi(`var')
	bysort pid (mi_`var'): replace `var' = `var'[1] if mi(`var')
}

*-------------------------------------------------------------------------------
*			0. Formating and Renaming Variables
*-------------------------------------------------------------------------------

duplicates drop pid round, force
isid pid round



tostring pid, gen(hhid)

bysort pid (date): gen first = _n == 1

ren admin1 admin1_str
encode admin1_str, gen(admin1)
tab admin1

gen month = month(date) +9 + 12*year(date) - 12*2019
tab month

duplicates drop pid date, force
bysort pid month (date): keep if _n == 1


*codebook effort face_problems happiness sleep difficulties
*stop
/*label define mental 1 "Never" 2 "Sometimes" 3 "Often/always"

foreach v in effort face_problems happiness sleep difficulties {
gen num_`v' = 1 + (`v' == "Sometimes") + 2*(`v' == "Often") if !mi(`v')
label values num_`v' mental
drop `v' 
ren num_`v' `v' 
}*/
*replace sleep = -1*sleep
*replace difficulties = -1*difficulties

*-------------------------------------------------------------------------------
*			1. Generating Variable Lists
*-------------------------------------------------------------------------------
macro drop depression
gl depression sleep difficulties face_problems

*-------------------------------------------------------------------------------
*			2. Generating Indices
*-------------------------------------------------------------------------------
foreach var in $depression {
	sum `var' if `ref'
	replace `var' = (`var' - `r(mean)')/`r(sd)'
	gen swt_`var' = `var'
}


factor   sleep difficulties face_problems  if  `ref' & admin1 == 3, ipf factor(1)
predict ___depression_fw if admin1 == 3



*-------------------------------------------------------------------------------
*          Unweighted indices
*-------------------------------------------------------------------------------
egen ___depression_nw = rowmean(sleep difficulties face_problems)


*-------------------------------------------------------------------------------
*          Inverse-covariance-weighted indices
*-------------------------------------------------------------------------------
gen sleep_r = sleep if admin1 == 3
gen difficulties_r = difficulties if admin1 == 3
gen face_problems_r = face_problems if admin1 == 3

icw_index (sleep_r difficulties_r face_problems_r), gen(___depression_icw)

